package gruppe7.servlets;

import gruppe7.data.UserController;
import gruppe7.tlrs.Station;
import gruppe7.tlrs.TLRSControl;
import gruppe7.util.Html;

import java.io.IOException;
import java.io.PrintWriter;

import javax.servlet.RequestDispatcher;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

public class EditStation extends HttpServlet {

    private static final long serialVersionUID = 1L;
    private Html htmlInclude = new Html();

    protected void doGet(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        PrintWriter out = response.getWriter();
        RequestDispatcher rd = request.getRequestDispatcher("main_links.jsp");
        UserController uc = UserController.getInstance();
        rd.include(request, response);

        out.println(htmlInclude.setHeader("Edit station"));
        out.println("<div id ='content'>");
        out.println("<div id='content-left'>");
        out.println("<h3><img src='images/edit.png' /> Edit Station</h3><br/>");

        if (uc.isValidSession(request)) {
            String stationName = request.getParameter("station");
            String oldName = stationName;
            TLRSControl tlrsC = TLRSControl.getInstance();
            Station station = tlrsC.getStation(oldName);
            int zone = tlrsC.getZone(stationName);
            out.println("<p>"
                    + "<table class='default'>"
                    + "<form action='EditStationInfo' method='post'>"
                    + "<tr><td>Edit name </td><td><input type='text' name='editedName' value='" + stationName + "'/></td></tr>"
                    + "<tr><td>Edit zone </td><td><input type='text' name ='editedZone' value='" + zone + "'/>"
                    + "<input type='hidden' name ='oldName' value='" + oldName + "'/></td></tr>"
                    + "<tr><td>Edit comment </td><td><input type='text' name='comment' value='" + station.getComment() + "'/></td></tr>"
                    + "<tr><td>Edit manager </td><td><input type='text' name='manager' value='" + station.getManager() + "'/></td></tr>"
                    + "<tr><td>Edit erection date </td><td><input type='text' name='erectiondate' value='" + station.getErectionDate() + "'/></td></tr>"
                    + "<tr><td></td><td align='right'><input class='submit' type='submit' value='Save'/></td></tr>"
                    + "</form>"
                    + "</table>"
                    + "</p>"
                    + "<br /><p><a href='javascript: history.go(-1)'>Back</a></P>");
        }
        out.println("</div></div>");
        out.println(htmlInclude.setFooter());

    }

    protected void doPost(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        doGet(request, response);
    }
}
